/* eslint-disable no-console */

const storageAccessToken = 'accessToken';
const storageRefreshToken = 'refreshToken';

export const storage = {
  getAccessToken: (): string => window.localStorage.getItem(storageAccessToken) as string,
  getRefreshToken: (): string => window.localStorage.getItem(storageRefreshToken) as string,

  setAccessToken: (token: string): void => {
    window.localStorage.setItem(storageAccessToken, token);
  },
  setRefreshToken: (token: string): void => {
    window.localStorage.setItem(storageRefreshToken, token);
  },

  clearTokens: (): void => {
    window.localStorage.removeItem(storageAccessToken);
    window.localStorage.removeItem(storageRefreshToken);
  },
};
const getItem = (key: string): string | null => {
  try {
    return localStorage.getItem(key) as string;
  } catch (error) {
    console.error('Error accessing localStorage:', error);
    return null;
  }
};

export const storageTokens = {
  getAccessToken: (): string | null => getItem(storageAccessToken),
  getRefreshToken: (): string | null => getItem(storageRefreshToken),
};
export const removeData = (key: string) => {
  try {
    localStorage.removeItem(key);
  } catch (e) {
    console.info(e);
  }
};

export const storeData = async (key: string, value: string) => {
  try {
    await localStorage.setItem(key, value);
  } catch (e) {
    // saving error
    console.info(e);
  }
};
